package com.rainkaze.IntelliDrawNote.ui;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.rainkaze.IntelliDrawNote.R;
import com.rainkaze.IntelliDrawNote.bean.Notepad;
import com.rainkaze.IntelliDrawNote.bean.TodoNote;
import com.rainkaze.IntelliDrawNote.databinding.ActivityEditTodoBinding;
import com.rainkaze.IntelliDrawNote.db.MyDbHelper;
import com.rainkaze.IntelliDrawNote.utils.TimeUtils;

import java.io.Serializable;
import java.util.Calendar;

public class TodoEditActivity extends AppCompatActivity {

    private ActivityEditTodoBinding binding;

    private MyDbHelper myDbHelper;

    private TodoNote todoNote;

    private TextView ddlTimeTv;
    private ImageView selectTimeIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding=ActivityEditTodoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Serializable serializable = getIntent().getSerializableExtra("todoNote");
        if (serializable != null){
            binding.titleTv.setText("修改待办");
            todoNote=(TodoNote) serializable;
            binding.contentEt.setText(todoNote.getContent());
            binding.ddlTimeTv.setText(todoNote.getDdlTime());
        }else{
            binding.titleTv.setText("添加待办");
        }

        ddlTimeTv=binding.ddlTimeTv;
        selectTimeIv=binding.selectTimeIv;
        selectTimeIv.setOnClickListener(v -> {
            // 获取当前时间
            final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            // 先弹出日期选择器
            new DatePickerDialog(v.getContext(), (view, year1, month1, dayOfMonth) -> {
                // 日期选好了，再弹出时间选择器
                new TimePickerDialog(v.getContext(), (timePicker, hourOfDay, minute) -> {
                    // 格式化日期时间
                    String timeStr = String.format("%04d年%02d月%02d日 %02d:%02d",
                            year1, month1 + 1, dayOfMonth, hourOfDay, minute);
                    // 设置到TextView
                    ddlTimeTv.setText(timeStr);
                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true).show();

            }, year, month, day).show();
        });


        binding.backIv.setOnClickListener(v -> finish());
        binding.clearIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.contentEt.setText("");
            }
        });

        myDbHelper=new MyDbHelper(this);
        binding.saveIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=binding.contentEt.getText().toString().trim();

                if(content.isEmpty()){
                    Toast.makeText(TodoEditActivity.this, "请输入待办内容", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 判断是新增还是修改
                if (todoNote==null){
                    todoNote=new TodoNote();
                    todoNote.setContent(content);
                    todoNote.setTime(TimeUtils.getTime());
                    todoNote.setDdlTime(ddlTimeTv.getText().toString());
                    myDbHelper.insertTodo(todoNote);
                    Toast.makeText(TodoEditActivity.this, "新增待办成功", Toast.LENGTH_SHORT).show();
                }else{
                    todoNote.setContent(content);
                    todoNote.setTime(TimeUtils.getTime());
                    todoNote.setDdlTime(ddlTimeTv.getText().toString());
                    myDbHelper.updateTodo(todoNote);
                    Toast.makeText(TodoEditActivity.this, "更新待办成功", Toast.LENGTH_SHORT).show();
                }

                setResult(RESULT_OK);
                finish();
            }
        });


    }
}